package com.example.util;

import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.config.Configure;
import com.deepoove.poi.data.RowRenderData;
import com.deepoove.poi.data.Rows;
import com.deepoove.poi.data.Tables;
import com.deepoove.poi.data.style.TableStyle;
import com.deepoove.poi.policy.HackLoopTableRenderPolicy;
import com.example.util.bean.PoiTlBean;

import java.io.FileOutputStream;
import java.util.*;

/**
 * @author 贾明祯
 * @ClassName TlWordUtil
 * @Date 2021/2/23 8:31
 * @Introduce
 */
public class TlWordUtil {
    public static void main(String[] args) {
        try {
            String path = Objects.requireNonNull(ReadFileUtil.class.getClassLoader().getResource("static/texta.docx")).getPath();
            List<PoiTlBean> poiTlBeans = new ArrayList<>();
            for(int i = 0;i<5;i++){
                PoiTlBean poiTlBean = new PoiTlBean();
                poiTlBean.setName("name"+i);
                poiTlBean.setAge("age"+i);
                poiTlBean.setEmail("email"+i);
                poiTlBean.setPassword("password"+i);
                poiTlBean.setCount(i+"");
                poiTlBeans.add(poiTlBean);
            }

//            RowRenderData row0 = Rows.of("姓名", "学历").textColor("FFFFFF")
//                    .bgColor("4472C4").center().create();
//            RowRenderData row1 = Rows.create("李四", "博士");
//            put("table1", Tables.create(row0, row1));

            HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();
            Configure config = Configure.builder().bind("poiTlBeans", policy).build();

            XWPFTemplate template = XWPFTemplate.compile(path,config).render(
                    new HashMap<String, Object>(){{

                        put("poiTlBeans", poiTlBeans);
                    }});
            template.writeAndClose(new FileOutputStream("D:/output.docx"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
